package com.market.manager.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.market.dao.SupplierDao;
import com.market.domain.Picture;
import com.market.domain.Product;
import com.market.domain.Supplier;
import com.market.manager.SupplierManager;

@Service("supplierManager")
public class SupplierManagerImpl extends
		GenericManagerImpl<SupplierDao, Supplier, Long> implements
		SupplierManager {
	@Autowired
	@Override
	public void setDao(SupplierDao supplierDao) {
		super.dao = supplierDao;

	}

	public Supplier fetchOne(Supplier supplier) {
		if ((supplier != null) && supplier.getIsNew() == false) {
			return this.fetchOne(supplier.getId());
		}
		return supplier;
	}

	@Override
	public Supplier fetchOne(Long id) {
		Supplier supplier = this.dao.findById(id);
		supplier.setProducts(this.fetchProducts(supplier));
		supplier.setPictures(this.fetchPictures(supplier));
		return supplier;
	}

	public List<Product> fetchProducts(Supplier supplier) {
		return this.dao.findProducts(supplier);
	}

	public List<Picture> fetchPictures(Supplier supplier) {

		return this.dao.findPictures(supplier);
	}

}
